package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 查询邮箱绑定的人数
     * @param mail
     * @return
     */
    @Select("select count(*) from user where email=#{email}")
int countByMail(@Param("email") String mail);

    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user (email,phone_number,identity,password,user_name) values " +
            " (#{email},#{phoneNumber},#{identity},#{password},#{userName})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(UserDO userDO);

    @Select("select * from user where email=#{email}")
    UserDO selectByEmail(@Param("email") String email);
@Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(@Param("phoneNumber") Encrypt encrypt);

@Select("<script>" +
        " select * from user " +
        " <if test=\"identityString!=null\">" +
        " where identity= #{identityString} " +
        " </if>" +
        " order by id desc" +
        " </script>")

    List<UserDO> selectUserListByIdentity(@Param("identityString") String identityString);


    @Select("<script>" +
            " select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExitsByIds(@Param("items") List<Long> userIds);

    @Select("<script>" +
            " select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items")List<Long> collect);
}
